package com.vids.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vids.mapper.ImageComparedDetailMapper;
import com.vids.model.entity.ImageComparedDetail;
import com.vids.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/images")
public class ImageFileController {

    @Resource
    ImageComparedDetailMapper detailMapper ;

    //picture/2024-06-24/19/face/42022201001320058103/420222010013200581030120240624192659_faceImage.jpg
    //http://11.32.32.62:8080/image/picture/2024-06-24/19/face/42022201001320058103/420222010013200581030120240624192715_faceBackgroundImage.jpg

    @ApiOperation(value = "查询图片详情", httpMethod = "GET", notes = "查询图片详情")
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String s_apeIpAddr,
                              @RequestParam(defaultValue = "") String s_imageType,
                              @RequestParam(defaultValue = "") String s_comparedHour,
                              @RequestParam(defaultValue = "ape") String s_imageFrom,
                              @RequestParam(defaultValue = "") String startTime,
                              @RequestParam(defaultValue = "") String endTime) {


        LambdaQueryWrapper<ImageComparedDetail> wrapper = Wrappers.<ImageComparedDetail>lambdaQuery();

        wrapper.orderByDesc(ImageComparedDetail::getId);

        if (StrUtil.isNotBlank(search)) {
            wrapper.like(ImageComparedDetail::getApeId, search);
            log.info("search="+ search);
        }

        if (StrUtil.isNotBlank(s_apeIpAddr)) {
            wrapper.eq(ImageComparedDetail::getApeIpAddr, s_apeIpAddr);
            log.info("s_apeIpAddr="+ s_apeIpAddr);
        }

        if (StrUtil.isNotBlank(s_imageType) && (!s_imageType.contains("99"))) {
            wrapper.eq(ImageComparedDetail::getImageType, Integer.parseInt(s_imageType));
            log.info("s_imageType="+ s_imageType);
        }


        if (StrUtil.isNotBlank(s_comparedHour) && (!s_comparedHour.contains("24"))) {
            wrapper.eq(ImageComparedDetail::getComparedHour, Integer.parseInt(s_comparedHour));
            log.info("s_comparedHour="+ s_comparedHour);
        }

        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            wrapper.between(ImageComparedDetail::getApeImageCreateTime, startTime, endTime);
            log.info("startTime="+ startTime);
            log.info("endTime="+ endTime);
        }else if (StrUtil.isNotBlank(startTime))
        {
            wrapper.ge(ImageComparedDetail::getApeImageCreateTime,startTime);
        }else if (StrUtil.isNotBlank(endTime))
        {
            wrapper.le(ImageComparedDetail::getApeImageCreateTime,startTime);
        }

        Page<ImageComparedDetail> detailPage = detailMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        String image_full_name="";
        String image_full_name2="";
        String imageTypeStr;

        String  viid_root = "viid_picture/";
        String  ape_root = "picture/";

        for(ImageComparedDetail detail: detailPage.getRecords())
        {
            //  目标类型 TargetType 1：人员，2：人脸，3：机动车，4：非机动车，5：物品，6：场景
            if (detail.getImageType() == 1)
            {
                imageTypeStr= "person";
            }
            else if (detail.getImageType() == 2)
            {
                imageTypeStr= "face";
            } else if (detail.getImageType() == 3)
            {
                imageTypeStr= "motor";
            }else if (detail.getImageType() == 4)
            {
                imageTypeStr= "non_motor";
            }else {
                imageTypeStr= "unknown";
            }

            detail.setImageTypeStr(imageTypeStr);

            LocalDateTime ldt = detail.getApeImageCreateTime();
            if (ldt != null) {
                String date_path = ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                image_full_name = ape_root + date_path + "/" + detail.getComparedHour() + "/" + imageTypeStr + "/" + detail.getApeId() + "/" + detail.getApeImage();
            }
            detail.setImageUrl(image_full_name);

            LocalDateTime ldt2 = detail.getViidImageCreateTime();
            if (ldt2 != null) {
                String date_path2 = ldt2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                image_full_name2 = viid_root + date_path2 + "/" + detail.getComparedHour() + "/" + imageTypeStr + "/" + detail.getApeId() + "/" + detail.getViidImage();
            }
            detail.setViidImageUrl(image_full_name2);

        }

        log.info("detailPage  total="+ detailPage.getTotal());
        log.info("detailPage  pages="+ detailPage.getPages());
        return Result.success(detailPage);
    }


}
